var production_state;
function managerView(){
	var managerView = 
	"<html>"+
	"<head>"+
	"<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>"+
	"<title>Insert title here</title>"+
	"<script type='text/javascript' src='menu.js'></script>"+
	"</head>"+
	"<body><form name='clock' style='padding-left: 145px'>"+
	"<font face='DS-Digital' size='5px'><b>작업자"+
	" <input type='text' id='worker'value='' style='font-family: DS-Digital; font-size: 30px; border: none; width: 160px' readonly='readonly'> 일시 :"+
	" <input class='' type='text' id='date' style='font-family: DS-Digital; font-size: 30px; border: none; width: 180px'readonly='readonly'>"+
	" 현재시각 : <input type='text' id='time' style='font-family: DS-Digital; font-size: 30px; border: none'readonly='readonly'></b></font> </form>"+
	"<br><div id='menu2'></div>"+
	"<div id=middle>"+
	"<div id='mbri'></div>"+
	"<div id='mana_charts' style='height: 300px ; width: 820px; float:Left'>"+
	"<input type='button' onclick='workorder_speed1()' value='1배속'><input type='button' onclick='workorder_speed2()' value='2배속'><input type='button' onclick='workorder_speed3()' value='10배속'><table border='1' bordercolor='black'>"+
	"<table>"+
	"	<tr><td>"+
	"	<table width='820px' height='100'>"+
	"		<tr>"+
	"			<td colspan='4' bgcolor='black' width='400' height='100%'><font color='white'>완성실적</font></td>"+
	"		</tr>"+
	"		<tr>"+
	"			<td width='150' bgcolor='lightgrey'></td><td width='150'bgcolor='lightgrey'>목표</td><td width='150'bgcolor='lightgrey'>실적</td><td width='150'bgcolor='lightgrey'>진척률</td>"+
	"		</tr>"+
	"		<tr>"+
	"			<td bgcolor='lightgrey'>PLC</td><td><input type='text' id='workorder_quantity' value='' style='border: none; width: 70px' readonly='readonly'></td><td><input type='text' id='production_total_quantity' value='' style='border: none; width: 70px' readonly='readonly'></td><td><input type='text' id='production_total_persent2' value='' maxlength='5' style='border: none; width: 70px' readonly='readonly'></td>"+
	"		</tr>"+
	"		<tr>"+
	"			<td bgcolor='lightgrey'>LOT</td><td></td><td><input type='text' id='production_quantity' value='' style='border: none; width: 70px' readonly='readonly'></td><td><input type='text' id='production_persent2' value='' maxlength='5' style='border: none; width: 70px' readonly='readonly'></td>"+
	"		</tr>"+
	"	</table>"+
	
	"	</td><td>"+
	"	<table width='410px' height='100'>"+
	"		<tr>"+
	"			<td colspan='4' bgcolor='black'><font color='white'>수불현황</font></td>"+
	"		</tr>"+
	"		<tr>"+
	"			<td width='150'bgcolor='lightgrey'>목표불량</td><td width='150'><input type='text' id='workorder_badquantity' value='' style='border: none; width: 70px' readonly='readonly'></td>"+
	"		</tr>"+
	"		<tr>"+
	"			<td bgcolor='lightgrey'>불량수량</td><td><input type='text' id='production_bad_quantity' value='' style='border: none; width: 70px' readonly='readonly'></td>"+
	"		</tr>"+
	"		<tr>"+
	"			<td bgcolor='lightgrey'>불량률</td><td><input type='text' id='bad_persent' value='' style='border: none; width: 70px' readonly='readonly'></td>"+
	"		</tr>"+
	"	</table>"+
	"	</td></tr>"+
	"	<tr><td>"+
	"	<table  width='820px' height='150'>"+
	"		<tr>"+
	"			<td colspan='5' bgcolor='black'><font color='white'>공정별 수불현황</font></td>"+
	"		</tr>"+
	"		<tr>"+
	"			<td td width='120' bgcolor='lightgrey'></td><td width='120' bgcolor='lightgrey'>1공정</td><td width='120' bgcolor='lightgrey'>2공정</td><td width='120' bgcolor='lightgrey'>3공정</td><td width='120' bgcolor='lightgrey'>4공정</td>"+
	"		</tr>"+
	"		<tr>"+
	"			<td td width='120' bgcolor='lightgrey'><input type='text' value='불량' style='border:none; width: 70px; background-color: lightgrey' readonly='readonly'></td><td><input type='text' width='120' id='production_bad_quantity1' value='' style='border: none; width: 70px' readonly='readonly'></td><td><input type='text' width='120' id='production_bad_quantity2' value='' style='border: none; width: 70px' readonly='readonly'></td><td><input type='text' width='120' id='production_bad_quantity3' value='' style='border: none; width: 70px' readonly='readonly'></td><td><input type='text' width='120' id='production_bad_quantity4' value='' style='border: none; width: 70px' readonly='readonly'></td>"+
	"		</tr>"+
	"		<tr>"+
	"			<td td width='120' bgcolor='lightgrey'>재가공</td><td><input type='text' width='120' id='production_bad_quantity11' value='' style='border: none; width: 70px' readonly='readonly'></td><td><input type='text' width='120' id='production_bad_quantity22' value='' style='border: none; width: 70px' readonly='readonly'></td><td><input type='text' width='120' id='production_bad_quantity33' value='' style='border: none; width: 70px' readonly='readonly'></td><td><input type='text' width='120' id='production_bad_quantity44' value='' style='border: none; width: 70px' readonly='readonly'></td>"+
	"		</tr>"+
	"		<tr>"+
	"			<td td width='120' bgcolor='lightgrey'>폐기</td><td><input type='text' width='120' id='production_bad_quantity111' value='' style='border: none; width: 70px' readonly='readonly'></td><td><input type='text' width='120' id='production_bad_quantity222' value='' style='border: none; width: 70px' readonly='readonly'></td><td><input type='text' width='120' id='production_bad_quantity333' value='' style='border: none; width: 70px' readonly='readonly'></td><td><input type='text' width='120' id='production_bad_quantity444' value='' style='border: none; width: 70px' readonly='readonly'></td>"+
	"		</tr>"+
	"	</table>"+
	"	</td><td>"+
	"	<table width='410px' height='150' >"+
	"		<tr>"+
	"			<td colspan='2' bgcolor='black'><font color='white'>불량</font></td>"+
	"		</tr>"+
	"		<tr>"+
	"			<td width='150' bgcolor='lightgrey'>총불량</td><td width='150'><input type='text' id='production_total_bad' value='' maxlength='5' style='border: none; width: 70px' readonly='readonly'></td>"+
	"		</tr>"+
	"		<tr>"+
	"			<td bgcolor='lightgrey'>재가공</td><td><font face='DS-Digital'><input type='text' id='production_total_bad2' value='' maxlength='5' style='border: none; width: 70px' readonly='readonly'></font></td>"+
	"		</tr>"+	
	"		<tr>"+
	"			<td bgcolor='lightgrey'>폐기</td><td><font face='DS-Digital'><input type='text' id='production_total_bad3' value='' maxlength='5' style='border: none; width: 70px' readonly='readonly'></font></td>"+
	"		</tr>"+	
	"	</table>"+
	"	</td></tr>"+
	"</table></div> " +
	"<div id='mana_charts2' style='width: 410px ; height: 300px; float:Left'>"+
	"</div>"+
	"<div id='hidden'><input type='hidden' id='production_bad_quantityf' value='0'><input type='hidden' id='production_total_quantity1' value='0'><input type='hidden' id='workorder_badquantity1' value='0'><input type='hidden' id='workorder_quantity1' value='0'><input type='hidden' id='bad_quantity1' value='0'><input type='hidden' id='bad_quantity11' value='0'><input type='hidden' id='bad_quantity2' value='0'><input type='hidden' id='bad_quantity22' value=''><input type='hidden' id='bad_quantity3' value=''><input type='hidden' id='bad_quantity4' value=''>";
	"</center></body>"+
	"</html>";
	$("#main").html(managerView);
	$("#menu2").html(menuBar2());
	production_manager();
	managerworker();
	clock();
	$.ajax({
		type : "POST",
		url : "login.do",
		data : "command=workorderCheck",
		dataType : "json",
		success : function(jsonData){
			var workorder_quantity=0;
			workorder_quantity= parseInt(jsonData.wvo.workorder_quantity);
			var workorder_badquantity=0;
			workorder_badquantity= parseInt(jsonData.wvo.workorder_badquantity);
			$("#workorder_quantity").val(workorder_quantity);
			$("#workorder_badquantity").val(workorder_badquantity);
			$("#workorder_quantity1").val(workorder_quantity);
			$("#workorder_badquantity1").val(workorder_badquantity);
		}
	});
	
}


function clock(){
	 data = new Date();
	 // 시간과 관련된 정보를 저장한다.
	 hours = data.getHours();
	 minutes = data.getMinutes();
	 seconds = data.getSeconds();
	 timeStr = hours;
	 timeStr += ((minutes < 10) ? ":0" : ":") + minutes;
	 timeStr += ((seconds < 10) ? ":0" : ":") + seconds;
	 // 폼의 시간을 표시하는 입력란에 문자열을 출력한다.
	 document.clock.time.value = timeStr;
	 // 일자와 관련된 정보를 저장한다.
	 months = data.getMonth() + 1;
	 days = data.getDate();
	 years = 2012+"/";
	 dateStr = ((years < 10) ? "/0" : "") + years;
	 dateStr += months+"/";
	 dateStr += ((days < 10) ? "/0" : "") + days;
	 // 폼의 일자를 표시하는 입력란에 문자열을 출력한다.
	 document.clock.date.value = dateStr;
	 // 1초마다 일자와 시간을 갱신한다.
	 Timer = setTimeout("clock()", 1000);
}

function managerworker(){
	document.clock.worker.value="MANAGER";
}

function production_manager(){
	$.ajax({
		type : "POST",
		url : "manager.do",
		data : "command=production_view",
		dataType : "json", 
		success : function(jsonData) {
			var production_quantity=0;
			production_quantity= parseInt(jsonData.production_map.production_quantity);
			var production_bad_quantity1=0;
			production_bad_quantity1= parseInt(jsonData.production_map.production_bad_quantity1)+parseInt(jsonData.production_map.production_bad_quantity11);
			var production_bad_quantity2=0;
			production_bad_quantity2= parseInt(jsonData.production_map.production_bad_quantity2)+parseInt(jsonData.production_map.production_bad_quantity22);
			var production_bad_quantity11=0;
			production_bad_quantity11= parseInt(jsonData.production_map.production_bad_quantity11);
			var production_bad_quantity22=0;
			production_bad_quantity22= parseInt(jsonData.production_map.production_bad_quantity22);
			var production_bad_quantity3=0;
			production_bad_quantity3= parseInt(jsonData.production_map.production_bad_quantity3);
			var production_bad_quantity4=0;
			production_bad_quantity4= parseInt(jsonData.production_map.production_bad_quantity4);
			var production_bad_quantity=0;
			production_bad_quantity= production_bad_quantity1+production_bad_quantity2+production_bad_quantity3+production_bad_quantity4;
			var production_total_quantity=0;
			production_total_quantity= production_quantity+production_bad_quantity1+production_bad_quantity2+production_bad_quantity3+production_bad_quantity4;
			production_state = jsonData.production_map.production_state;
			var workorder_quantity=0;
			workorder_quantity= $("#workorder_quantity").val();
			var production_total_persent=0;
			production_total_persent= ((production_total_quantity/workorder_quantity)*100).toFixed(1)+'%';
			var production_persent=0;
			production_persent= ((production_quantity/workorder_quantity)*100).toFixed(1)+'%';
			$("#production_quantity").val(production_quantity);
			$("#production_bad_quantity1").val(production_bad_quantity1);
			$("#production_bad_quantity2").val(production_bad_quantity2);
			$("#production_bad_quantity11").val(production_bad_quantity11);
			$("#production_bad_quantity22").val(production_bad_quantity22);
			$("#production_bad_quantity3").val(production_bad_quantity3);
			$("#production_bad_quantity4").val(production_bad_quantity4);
			$("#production_bad_quantity33").val(production_bad_quantity3);
			$("#production_bad_quantity44").val(production_bad_quantity4);
			$("#production_bad_quantity111").val(production_bad_quantity1-production_bad_quantity11);
			$("#production_bad_quantity222").val(production_bad_quantity2-production_bad_quantity22);
			$("#production_bad_quantity333").val(0);
			$("#production_bad_quantity444").val(0);
			$("#production_bad_quantity").val(production_bad_quantity);
			$("#production_bad_quantityf").val(production_bad_quantity);
			
			$("#production_total_quantity").val(production_total_quantity);
			$("#production_total_quantity1").val(production_total_quantity);
			$("#production_total_persent2").val(production_total_persent);
			$("#production_persent2").val(production_persent);

			var production_total_bad = production_bad_quantity1+production_bad_quantity2+production_bad_quantity3+production_bad_quantity4
			$("#production_total_bad").val(production_total_bad);
			$("#production_total_bad2").val(production_bad_quantity11+production_bad_quantity22+production_bad_quantity3+production_bad_quantity4);
			$("#production_total_bad3").val((production_bad_quantity1-production_bad_quantity11)+(production_bad_quantity2-production_bad_quantity22));
			$("#bad_quantity1").val(production_bad_quantity1);
			$("#bad_quantity2").val(production_bad_quantity2);
			$("#bad_quantity11").val(production_bad_quantity11);
			$("#bad_quantity22").val(production_bad_quantity22);
			$("#bad_quantity3").val(production_bad_quantity3);
			$("#bad_quantity4").val(production_bad_quantity4);
			var workorder_badquantity2 = $("#workorder_badquantity").val();
			var bad_persent = production_total_bad/workorder_badquantity2*100;
			$("#bad_persent").val(bad_persent.toFixed(1)+'%');
			var total = workorder_quantity-(production_quantity+production_bad_quantity);
			$("#chat_val1").val(total);
			$("#chat_val2").val(production_quantity);
			$("#chat_val3").val(production_bad_quantity);
			if(production_state=='start'){
				$("#footer").html("<img src='img/start.gif'>");
			}else{
				$("#footer").html("<img src='img/stop.gif'>");
			}
			production_manager();
		}		  
	});
}
function buyForm(){
	$("#mbri").html("<div id='buy_form'></div>");
	var soap_base1=0;
	var oil1=0;
	var hardener1=0;
	var wrapper1=0;
	$.ajax({
		type : "POST",
		url : "manager.do",
		data : "command=itemDataList",
		dataType : "json", 
		success : function(jsonData) {	
			$.each(jsonData.itemlist, function(index, value){
				if(value.item_id=="soap_base")
					soap_base1 = value.item_oriquantity;						
				if(value.item_id=="oil")
					oil1 = value.item_oriquantity;
				if(value.item_id=="hardener")
					hardener1 = value.item_oriquantity;
				if(value.item_id=="wrapper")
					wrapper1 = value.item_oriquantity;				
			});
			var form="";
			form+="원료주문";
			form+="<table border='0'><tr><td>";
			form+="<table border='0'>";
			form+="<tr><td>비누베이스</td><td>"+soap_base1+"</td></tr>";
			form+="<tr><td>오일</td><td>"+oil1+"</td></tr>";
			form+="<tr><td>경화제</td><td>"+hardener1+"</td></tr>";
			form+="<tr><td>포장지</td><td>"+wrapper1+"</td>";
			form+="</tr></table></td>";
			form+="<td><table>";
			form+="<td><input type='text' id='soap_base' placeholder='비누베이스(Kg)'><br>";
			form+="<input type='text' id='oil' placeholder='오일(L)'><br>";
			form+="<input type='text' id='hardener' placeholder='경화제(L)'><br>";
			form+="<input type='text' id='wrapper' placeholder='포장지(ea)'></td>";
			form+="<td><input type='button' id='buy' style='height: 85px; width: 70px' value='주문'></input></td>";	
			form+="</tr></table></td></tr></table>";
			$("#buy_form").html(form);			
		}
	});	
}
var speed=1500;
function workorder_speed1() {
	speed=1500;
}
function workorder_speed2() {
	speed=700;
}
function workorder_speed3() {
	speed=100;
}
function start(){
	var startckeck = $("#production_total_quantity").val();
	if(startckeck!=0){
		alert("지시가 없거나 이미 시작중입니다.");
	}else{
	var workorder_quantity = $("#workorder_quantity").val();
	$.ajax({
		type : "POST",
		url : "manager.do",
		data : "command=workProduction&workorder_quantity="+workorder_quantity+"&speed="+speed,
		dataType : "json", 
		success : function(jsonData) {	
		}
	});
	}
}
$(function(){		
	$("#main").on("click","#buy",function(){
		var soap_base = $("#soap_base").val();
		var oil = $("#oil").val();
		var hardener = $("#hardener").val();
 		var wrapper = $("#wrapper").val();
 		if(soap_base==""){
 			soap_base=0;
 		}if(oil==""){
 			oil=0;
 		}if(hardener==""){
 			hardener=0;
 		}if(wrapper==""){
 			wrapper=0;
 		}
		$.ajax({
			type : "POST",
			url : "manager.do",
			data : "command=itemDataInsert&soap_base="+soap_base+"&oil="+oil+"&hardener="+hardener+"&wrapper="+wrapper
		});
		$("#buy_form").html(function(){
			return "";
		});
	});	
}); 